﻿using ClusterAware;
using ClusterAwareAdmin;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ClusterAwareProperties;
using System.Linq;

namespace ClusterAwareTest
{


    /// <summary>
    ///This is a test class for GroupMethodsTest and is intended
    ///to contain all GroupMethodsTest Unit Tests
    ///</summary>
    [TestClass]
    public class GroupMethodsTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        private static ClusterManager _manager;

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize]
        public static void ClassTestInitialize(TestContext testContext)
        {
            var managerInit = new ClusterManagerAdmin
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            managerInit.Connect();
            if (!managerInit.Groups.ExistGroup(SharedConsts.NewGroupTest))
            {
                managerInit.Groups.Create(SharedConsts.NewGroupTest);
            }
            if (!managerInit.Groups.ExistGroup(SharedConsts.GroupTest))
            {
                managerInit.Groups.Create(SharedConsts.GroupTest);
            }
            if (managerInit.OperationSystem == WindowsOperationSystem.Windows2008 || managerInit.OperationSystem == WindowsOperationSystem.Windows2008R2)
            {
                var node = managerInit.Groups.GetActiveNode(SharedConsts.GroupTest).Name;
                managerInit.Groups.SetPreferredOwners(SharedConsts.GroupTest, new[] { node });
            }
            if (!managerInit.Resources.ExistResource(SharedConsts.ResourceDep))
            {
                managerInit.Resources.Create(SharedConsts.GroupTest, SharedConsts.ResourceDep, ResourceTypeNamesConstants.ResourcetypeGenericApplication, false);
                var res = managerInit.Resources.GetResource(SharedConsts.ResourceDep);
                res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe");
                res.PrivateProperties.SetPropertyValue("CurrentDirectory", "C:\\");
                res.PrivateProperties.SetPropertyValue("InteractWithDesktop", true);
                managerInit.Resources.Save(res);
            }
            if (!managerInit.Resources.ExistResource(SharedConsts.ResourceTest))
            {
                managerInit.Resources.Create(SharedConsts.GroupTest, SharedConsts.ResourceTest, ResourceTypeNamesConstants.ResourcetypeGenericApplication, false);
                var res = managerInit.Resources.GetResource(SharedConsts.ResourceTest);
                res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe");
                res.PrivateProperties.SetPropertyValue("CurrentDirectory", "C:\\");
                res.PrivateProperties.SetPropertyValue("InteractWithDesktop", true);
                managerInit.Resources.Save(res);
                managerInit.Resources.AddDependency(SharedConsts.ResourceTest, SharedConsts.ResourceDep);
            }
            managerInit.Dispose();
        }
        //
        //Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup]
        public static void ClassTestCleanup()
        {
            var managerInit = new ClusterManagerAdmin
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            managerInit.Connect();
            if (managerInit.Resources.ExistResource(SharedConsts.ResourceDep))
            {
                managerInit.Resources.Delete(SharedConsts.ResourceDep);
            }
            if (managerInit.Resources.ExistResource(SharedConsts.ResourceTest))
            {
                managerInit.Resources.Delete(SharedConsts.ResourceTest);
            }
            if (managerInit.Groups.ExistGroup(SharedConsts.NewGroupTest))
            {
                managerInit.Groups.Delete(SharedConsts.NewGroupTest);
            }
            if (managerInit.Groups.ExistGroup(SharedConsts.GroupTest))
            {
                managerInit.Groups.Delete(SharedConsts.GroupTest);
            }
            managerInit.Dispose();
        }
        //
        //Use TestInitialize to run code before running each test
        [TestInitialize]
        public void Initialize()
        {
            _manager = new ClusterManager
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            _manager.Connect();
        }
        //
        //Use TestCleanup to run code after each test has run
        [TestCleanup]
        public void Cleanup()
        {
            _manager.Dispose();
        }
        //
        #endregion

        /// <summary>
        ///A test for TakeOffline
        ///</summary>
        [TestMethod]
        public void TakeOfflineTest()
        {
            _manager.CacheInstances = true;
            var target = new GroupMethods(_manager);
            target.TakeOffline(SharedConsts.GroupTest, 10);
            Assert.AreEqual(StateGroupResources.Offline, target.GetGroup(SharedConsts.GroupTest).State);
            _manager.CacheInstances = false;
            target = new GroupMethods(_manager);
            target.TakeOffline(SharedConsts.GroupTest, 10);
            Assert.AreEqual(StateGroupResources.Offline, target.GetGroup(SharedConsts.GroupTest).State);
        }

        /// <summary>
        ///A test for Save
        ///</summary>
        [TestMethod]
        public void SaveTest()
        {
            _manager.CacheInstances = true;
            var target = new GroupMethods(_manager);
            var grpClass = target.GetGroup(SharedConsts.GroupTest);
            var olddes = grpClass.Description;
            grpClass.Description = "SaveTest";
            const string expected = "SaveTest";
            var actual = target.Save(grpClass);
            Assert.AreEqual(expected, actual.Description);
            grpClass.Description = olddes;
            actual = target.Save(grpClass);
            Assert.AreEqual(olddes, actual.Description);
            _manager.CacheInstances = false;
            target = new GroupMethods(_manager);
            grpClass = target.GetGroup(SharedConsts.GroupTest);
            olddes = grpClass.Description;
            grpClass.Description = "SaveTest";
            actual = target.Save(grpClass);
            Assert.AreEqual(expected, actual.Description);
            grpClass.Description = olddes;
            actual = target.Save(grpClass);
            Assert.AreEqual(olddes, actual.Description);
        }

        /// <summary>
        ///A test for MoveToNewNode
        ///</summary>
        [TestMethod]
        public void MoveToNewNodeTest()
        {
            _manager.CacheInstances = true;
            var target = new GroupMethods(_manager);
            var nodeactual = target.GetActiveNode(SharedConsts.GroupTest);
            foreach (var node in _manager.Nodes.GetNodes())
            {
                target.MoveToNewNode(SharedConsts.GroupTest, node.Name, 20);
            }
            target.MoveToNewNode(SharedConsts.GroupTest, nodeactual.Name, 20);
            Assert.AreEqual(nodeactual.Name, target.GetActiveNode(SharedConsts.GroupTest).Name);
            _manager.CacheInstances = false;
            target = new GroupMethods(_manager);
            nodeactual = target.GetActiveNode(SharedConsts.GroupTest);
            foreach (var node in _manager.Nodes.GetNodes())
            {
                target.MoveToNewNode(SharedConsts.GroupTest, node.Name, 20);
            }
            target.MoveToNewNode(SharedConsts.GroupTest, nodeactual.Name, 20);
            Assert.AreEqual(nodeactual.Name, target.GetActiveNode(SharedConsts.GroupTest).Name);
        }

        /// <summary>
        ///A test for GetActiveNode
        ///</summary>
        [TestMethod]
        public void GetActiveNodeTest()
        {
            _manager.CacheInstances = true;
            var target = new GroupMethods(_manager);
            var actual = target.GetActiveNode(SharedConsts.GroupTest);
            Assert.AreNotEqual(null, actual);
            _manager.CacheInstances = false;
            target = new GroupMethods(_manager);
            actual = target.GetActiveNode(SharedConsts.GroupTest);
            Assert.AreNotEqual(null, actual);
        }

        /// <summary>
        ///A test for GetResources
        ///</summary>
        [TestMethod]
        public void GetResourcesTest()
        {
            _manager.CacheInstances = true;
            var target = new GroupMethods(_manager);
            var actual = target.GetResources(SharedConsts.GroupTest);
            Assert.AreNotEqual(0, actual.Count());
            var resactual = actual.Select(r => r.Name == SharedConsts.ResourceTest).FirstOrDefault();
            Assert.AreNotEqual(null, resactual);
            _manager.CacheInstances = false;
            target = new GroupMethods(_manager);
            actual = target.GetResources(SharedConsts.GroupTest);
            Assert.AreNotEqual(0, actual.Count());
            resactual = actual.Select(r => r.Name == SharedConsts.ResourceTest).FirstOrDefault();
            Assert.AreNotEqual(null, resactual);
        }

        /// <summary>
        ///A test for GetPreferredNodes
        ///</summary>
        [TestMethod]
        public void GetPreferredNodesTest()
        {
            ClassTestInitialize(TestContext);
            _manager.CacheInstances = true;
            var target = new GroupMethods(_manager);
            var nodes = target.GetPreferredNodes(SharedConsts.GroupTest);
            if (_manager.OperationSystem == WindowsOperationSystem.Windows2008 || _manager.OperationSystem == WindowsOperationSystem.Windows2008R2)
            {
                Assert.AreNotEqual(0, nodes.Count());
            }
            _manager.CacheInstances = false;
            target = new GroupMethods(_manager);
            nodes = target.GetPreferredNodes(SharedConsts.GroupTest);
            if (_manager.OperationSystem == WindowsOperationSystem.Windows2008 || _manager.OperationSystem == WindowsOperationSystem.Windows2008R2)
            {
                Assert.AreNotEqual(0, nodes.Count());
            }
        }

        /// <summary>
        ///A test for GetGroups
        ///</summary>
        [TestMethod]
        public void GetGroupsTest()
        {
            ClassTestInitialize(TestContext);
            _manager.CacheInstances = true;
            var target = new GroupMethods(_manager);
            var actual = target.GetGroups();
            Assert.AreNotEqual(0, actual.Count());
            var grpactual = actual.Select(r => r.Name == SharedConsts.GroupTest).FirstOrDefault();
            Assert.AreNotEqual(null, grpactual);
            _manager.CacheInstances = false;
            target = new GroupMethods(_manager);
            actual = target.GetGroups();
            Assert.AreNotEqual(0, actual.Count());
            grpactual = actual.Select(r => r.Name == SharedConsts.GroupTest).FirstOrDefault();
            Assert.AreNotEqual(null, grpactual);
        }

        /// <summary>
        ///A test for GetGroup
        ///</summary>
        [TestMethod]
        public void GetGroupTest()
        {
            _manager.CacheInstances = true;
            var target = new GroupMethods(_manager);
            var actual = target.GetGroup(SharedConsts.GroupTest);
            Assert.AreNotEqual(null, actual);
            Assert.AreEqual(SharedConsts.GroupTest, actual.Name);
            _manager.CacheInstances = false;
            target = new GroupMethods(_manager);
            actual = target.GetGroup(SharedConsts.GroupTest);
            Assert.AreNotEqual(null, actual);
            Assert.AreEqual(SharedConsts.GroupTest, actual.Name);
        }

        /// <summary>
        ///A test for ExistGroup
        ///</summary>
        [TestMethod]
        public void ExistsGroupTest()
        {
            _manager.CacheInstances = true;
            var target = new GroupMethods(_manager);
            var result = target.ExistGroup(SharedConsts.GroupTest);
            Assert.AreEqual(true, result);
            result = target.ExistGroup(SharedConsts.GroupTest + "NotExist");
            Assert.AreEqual(false, result);
            _manager.CacheInstances = false;
            target = new GroupMethods(_manager);
            result = target.ExistGroup(SharedConsts.GroupTest);
            Assert.AreEqual(true, result);
            result = target.ExistGroup(SharedConsts.GroupTest + "NotExist");
            Assert.AreEqual(false, result);
        }

        /// <summary>
        ///A test for BringOnline
        ///</summary>
        [TestMethod]
        public void BringOnlineTest()
        {
            ClassTestInitialize(TestContext);
            _manager.CacheInstances = true;
            var target = new GroupMethods(_manager);
            target.BringOnline(SharedConsts.GroupTest, 10);
            Assert.AreEqual(StateGroupResources.Online, target.GetGroup(SharedConsts.GroupTest).State);
            _manager.CacheInstances = false;
            target = new GroupMethods(_manager);
            target.BringOnline(SharedConsts.GroupTest, 10);
            Assert.AreEqual(StateGroupResources.Online, target.GetGroup(SharedConsts.GroupTest).State);
        }

        /// <summary>
        ///A test for GroupMethods Constructor
        ///</summary>
        [TestMethod]
        public void GroupMethodsConstructorTest()
        {
            _manager.CacheInstances = true;
            var target = new GroupMethods(_manager);
            Assert.AreNotEqual(null, target);
            _manager.CacheInstances = false;
            target = new GroupMethods(_manager);
            Assert.AreNotEqual(null, target);
        }
    }
}
